﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 版权所有 (c) 2022 北京超维景生物科技有限公司 保留所有权利。
 * CLR版本：4.0.30319.42000
 * 文件名：Kernel
 * 
 * 创建者：huangyang
 * 电子邮箱：huangyang@tvscope.cn
 * 创建时间：2023/2/8 14:05:39
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ImageK.Util;

namespace ImageK.IO
{
    public class OpenDialog
    {
        private string _dir;
        private string _name;
        private bool _recordPath;
        private static string _defaultDirectory;
        // private static Frame sharedFrame;
        private string title;
        private static string _lastDir, _lastName;
        private static bool _defaultDirectorySet;

        /// <summary>
        /// Displays a file open dialog with 'title' as
        /// the title. If 'path' is non-blank, it is
        /// used and the dialog is not displayed. Uses
        /// and updates the ImageJ default directory.
        /// </summary>
        public OpenDialog(string title, string path)
        {
            string macroOptions = Macro.GetOptions();
            if (macroOptions != null && (path == null || path.Equals("")))
            {
                path = Macro.getValue(macroOptions, title, path);
                if (path == null || path.Equals(""))
                    path = Macro.getValue(macroOptions, "path", path);
                if ((path == null || path.Equals("")) && title != null && title.Equals("Open As String"))
                    path = Macro.getValue(macroOptions, "OpenAsString", path);
                path = lookupPathVariable(path);
            }
            
            if (string.IsNullOrEmpty(path))
            {
                if (Prefs.useJFileChooser)
                    jOpen(title, getDefaultDirectory(), null);
                else
                    open(title, getDefaultDirectory(), null);
                if (_name != null)
                    setDefaultDirectory(ref _dir);
                this.title = title;
                _recordPath = true;
            }
            else
            {
                DecodePath(path);
                _recordPath = IJ.MacroRunning();
            }
        }

        void DecodePath(string path)
        {
            int i = path.LastIndexOf('/');
            if (i == -1)
                i = path.LastIndexOf('\\');
            if (i > 0)
            {
                _dir = path.Substring(0, i + 1);
                _name = path.Substring(i + 1);
            }
            else
            {
                _dir = "";
                _name = path;
            }
        }

        /** Returns the selected directory. */
        public string GetDirectory()
        {
            _lastDir = _dir;
            return _dir;
        }

        /** Returns the selected file name. */
        public string GetFileName()
        {
            if (_name != null)
            {
                //todo:
                // if (Recorder.record && recordPath && _dir != null)
                //     Recorder.recordPath(title, _dir + _name);
                _lastName = _name;
            }
            return _name;
        }

        public static string lookupPathVariable(string path)
        {
            throw new NotImplementedException();
        }

        // Uses JFileChooser to display file open dialog box.
        void jOpen(string title, string path, string fileName)
        {
            throw new NotImplementedException();
        }

        void open(string title, string path, string fileName)
        {
            OpenFileDialog fd = new OpenFileDialog();
            if(fd.ShowDialog() == DialogResult.OK)
            {
                var fullPath = fd.FileName;
                _dir = PathUtil.GetDir(fullPath);
                _name = PathUtil.GetName(fullPath);
            }
        }

        /** Returns the current working directory as a string
        ending in the separator character ("/" or "\"), or
        an empty or null string. */
        public static string getDefaultDirectory()
        {
            return AppDomain.CurrentDomain.BaseDirectory;
        }

        /** Sets the current working directory.
	     * @see ij.plugin.frame.Editor#setDefaultDirectory
	    */
        public static void setDefaultDirectory(ref string dir)
        {
            dir = PathUtil.AddSeparator(dir);
            _defaultDirectory = dir;
            _defaultDirectorySet = true;
        }

        /** Returns the path to the directory that contains the last file
		 opened or saved, or null if a file has not been opened or saved. */
        public static String getLastDirectory()
        {
            return _lastDir;
        }

        /** Sets the path to the directory containing the last file opened by the user. */
        public static void setLastDirectory(String dir)
        {
            _lastDir = dir;
        }

        /** Returns the name of the last file opened by the user
		using a file open or file save dialog, or using drag and drop.
		Returns null if the users has not opened a file. */
        public static String getLastName()
        {
            return _lastName;
        }

        /** Sets the name of the last file opened by the user. */
        public static void setLastName(String name)
        {
            _lastName = name;
        }
    }
}
